package com.utc.cards.model;

import jade.core.AID;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.utc.cards.model.deck.Deck;
import com.utc.cards.model.game.Fold;
import com.utc.cards.model.game.IGame;
import com.utc.cards.model.player.IPlayer;

public class HostModel
{
    private static HostModel instance;

    /**
     * le jeu actuellement chargé
     */
    private IGame _game;
    /**
     * l'état actuel des mains des joueurs
     */
    private Map<IPlayer, Map<String, Deck>> _playerHands = new HashMap<IPlayer, Map<String, Deck>>();
    /**
     * l'état actuel des cartes de l'hôte (pioche)
     */
    private Map<String, Deck> _hostDecks = new HashMap<String, Deck>();
    /**
     * les plis précédents -> avoir la possibilité de lier un pli à un joueur?
     */
    private Stack<Fold> _oldFolds = new Stack<Fold>();
    /**
     * le pli courant
     */
    private Fold _currentFold;
    /**
     * les scores pour chaque manche
     */
    private Scores scores = new Scores();

    /**
     * assoc AID/login pour chacun
     */
    private Map<String, AID> _playersMap = new HashMap<String, AID>();

    private ArrayList<PlayerModel> iaPlayerModels = new ArrayList<PlayerModel>();

    private boolean ruleIsAvailable = false;;

    public HostModel()
    {
        super();
        instance = this;
    }

    public static HostModel Instance()
    {
        return instance;
    }

    public IGame getGame()
    {
        return _game;
    }

    public void setGame(IGame game)
    {
        this._game = game;
    }

    public Map<IPlayer, Map<String, Deck>> getPlayerHands()
    {
        return _playerHands;
    }

    public void setPlayerHands(Map<IPlayer, Map<String, Deck>> playerHands)
    {
        this._playerHands = playerHands;
    }

    public Map<String, Deck> getHostDecks()
    {
        return _hostDecks;
    }

    public void setHostDecks(Map<String, Deck> hostDecks)
    {
        this._hostDecks = hostDecks;
    }

    public Stack<Fold> getOldFolds()
    {
        return _oldFolds;
    }

    public void setOldFolds(Stack<Fold> oldFolds)
    {
        this._oldFolds = oldFolds;
    }

    public Fold getCurrentFold()
    {
        return _currentFold;
    }

    public void setCurrentFold(Fold currentFold)
    {
        this._currentFold = currentFold;
    }

    public Map<String, AID> getPlayersMap()
    {
        return _playersMap;
    }

    public Scores getScores()
    {
        return scores;
    }

    public void setScores(Scores scores)
    {
        this.scores = scores;
    }

    public ArrayList<PlayerModel> getIaPlayerModels()
    {
        return iaPlayerModels;
    }

    public void setIaPlayerModels(ArrayList<PlayerModel> iaPlayerModels)
    {
        this.iaPlayerModels = iaPlayerModels;
    }

    public boolean isRuleAvailable()
    {
        return this.ruleIsAvailable;
    }

    public void setRuleAvailability(boolean b)
    {
        this.ruleIsAvailable = b;
    }

    private List<String> readyPlayerList = new ArrayList<String>();

    public List<String> getReadyList()
    {
        return readyPlayerList;
    }

    private static Logger log = LoggerFactory.getLogger(HostModel.class);

    public boolean isIAPlayerAgentsAvailable()
    {
        int readyIAPlayerAgents = 0;
        for (String iaPlayer : getReadyList())
        {
            if (iaPlayer.contains("Joueur IA"))
            {
                readyIAPlayerAgents++;
            }
        }
        log.debug("Ready IAs : {}/{}", readyIAPlayerAgents, getIaPlayerModels().size());
        return readyIAPlayerAgents == getIaPlayerModels().size();
    }

    public boolean isHumanPlayerAgentsAvailable()
    {
        int readyHumanPlayerAgent = 0;
        for (String humanPlayer : getReadyList())
        {
            if (!humanPlayer.contains("Joueur IA"))
            {
                readyHumanPlayerAgent++;
            }
        }
        log.debug("Ready Humans : {}/{}", readyHumanPlayerAgent, getGame().getPlayers().size() - getIaPlayerModels().size());
        return readyHumanPlayerAgent == (getGame().getPlayers().size() - getIaPlayerModels().size());
    }

}
